const path = require('path');
const glob = require('glob');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin')
const postcssImport = require('postcss-import');
const cssnano = require('cssnano');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const TerserJSPlugin = require('terser-webpack-plugin');
const OptimizeAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const postcssPresetEnv = require('postcss-preset-env');
const CopyPlugin = require('copy-webpack-plugin');
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const devMode = process.env.NODE_ENV != 'production';

const webpackConfig = {
  entry: {
    main: './src/main.js',
    vendor: ['vue'],
    plugin: ['vue-router', 'axios']
  },
  output: {
    path: path.join(__dirname, 'dist'),
    filename: devMode ? 'assets/[name].[hash:8].js' : 'assets/[name].[contenthash:8].js'
  },
  mode: devMode ? 'development' : 'production',
  module: {
    rules: [
      {
        test: /\.html?$/,
        use: [
          {
            loader: 'html-loader',
            options: {
              minimize: devMode ? false : true
            }
          }
        ]
      },
      {
        test: /\.(less|css)$/,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader',
          'postcss-loader',
          'less-loader',
        ]
      },
      {
        test: /\.js$/,
        exclude: /node_modules/,
        include: [path.join(__dirname, 'src')],
        use: {
          loader: 'babel-loader?cacheDirectory'
        }
      },
      { test: /\.vue$/, use: 'vue-loader' },
      {
        test: /\.(png|bmp|jpe?g|gif|svg|woff2?|eot|ttf|otf)(\?.*)?$/,
        use: {
          loader: 'url-loader',
          options: {
            limit: 8192,
            publicPath: "../../",
            name: 'assets/[name].[hash:7].[ext]'
          }
        }
      }
    ]
  },
  
  optimization: {
    minimizer: [
      new CssMinimizerPlugin(),
    ],
  },

  resolve: {
    extensions: ['.js', '.json', '.css', '.less', '.vue'],
    // modules: [path.join(__dirname, 'src'), path.join(__dirname, 'node_modules')],
    alias: {
      '@': path.join(__dirname, 'src'),
      '@assets': path.join(__dirname, 'src/assets'),
      '@components': path.join(__dirname, 'src/components'),
      '@mods': path.join(__dirname, 'src/mods'),
      "@styles": path.join(__dirname, 'src/styles'),
      '@views': path.join(__dirname, 'src/views')
    }
  },
  externals: {
    vue: 'Vue'
  },
  devtool: devMode ? 'source-map' : 'cheap-source-map',
  devServer: {
    compress: true,
    noInfo: true,
    inline: true,
    hot: true,
    quiet: true,
    clientLogLevel: 'warning',
    host: '0.0.0.0',
    progress: true,
    proxy: {
      "/api": {
        target: "http://localhost:3000"
      }
    }
  },

  plugins: [
    new webpack.ProgressPlugin(),
    new VueLoaderPlugin(),
    new CopyPlugin({
      patterns: [
        { from: 'src/assets', to: 'assets' },
        { from: 'src/static', to: 'static' }
      ]
    }),
    devMode ? new webpack.HotModuleReplacementPlugin()
      : new CleanWebpackPlugin(),
    new MiniCssExtractPlugin({
      filename: devMode ? 'assets/[name].css' : 'assets/[name].[contenthash:8].css',
      chunkFilename: devMode ? 'assets/[id].css' : 'assets/[id].[contenthash:8].css'
    }),
    new HtmlWebpackPlugin({
      template: 'src/index.html',
      inject: 'body',
      filename: 'index.html'
    })
  ]
}

module.exports = webpackConfig;
